//
//  CommentCell.m
//  xpkc
//
//  Created by Mac mini 2014 on 14/12/19.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "CommentCell.h"

@implementation CommentCell
{
	UIView *_starBaseView;
}
- (void)awakeFromNib {
    // Initialization code
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
	if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
		[self createUI];
	}
	return self;
}
-(void)createUI
{
	_headImageView = [[UIImageView alloc]init];
	_userNameLabel = [[UILabel alloc]init];
	_timeLabel = [[UILabel alloc]init];
	_starBaseView = [[UIView alloc]init];
	_contentLabel = [[UILabel alloc]init];
	
	[self.contentView addSubview:_headImageView];
	[self.contentView addSubview:_userNameLabel];
	[self.contentView addSubview:_timeLabel];
	[self.contentView addSubview:_starBaseView];
	[self.contentView addSubview:_contentLabel];
}
-(void)setCellFrame
{
	CGFloat imageH = 35;

	_headImageView.frame = CGRectMake(20, 11, imageH, imageH);
	_headImageView.layer.cornerRadius = imageH/2.0;
	_headImageView.clipsToBounds = YES;
	_headImageView.backgroundColor = [UIColor cyanColor];
	
	_userNameLabel.frame = CGRectMake(CGRectGetMaxX(_headImageView.frame)+15, 10, 100, 9);
	_userNameLabel.textColor = [UIColor darkGrayColor];
	_userNameLabel.font = [UIFont systemFontOfSize:9];
	
	_timeLabel.frame = CGRectMake(SCREEN_WIDTH - 200, 10, 200, 10);
	_timeLabel.textColor = [UIColor lightGrayColor];
	_timeLabel.font = [UIFont systemFontOfSize:10];
	
	_starBaseView.frame = CGRectMake(SCREEN_WIDTH - 80,  10, 58, 10);
	for (int i = 0; i<5; i++) {
		UIImageView *starImageView = [[UIImageView alloc]init];
		starImageView.frame = CGRectMake(12*i, 0, 10, 10);
		starImageView.image = [UIImage imageNamed:@"star_empty.png"];
		starImageView.tag = i+1;
		[_starBaseView addSubview:starImageView];
	}
	[self setStarImageViewWithNumber:_starCount];

	CGSize contentSize = [_contentLabel.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - _headImageView.bounds.size.width - 35 - 30, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]} context:nil].size;
	_contentLabel.frame = CGRectMake(CGRectGetMaxX(_headImageView.frame)+15, CGRectGetMaxY(_userNameLabel.frame) + 10, contentSize.width, contentSize.height);
	_contentLabel.numberOfLines = 0;
	_contentLabel.textColor = [UIColor darkGrayColor];
	_contentLabel.font = [UIFont systemFontOfSize:11];
}
-(void)setStarImageViewWithNumber:(NSString *)numStr;
{
	CGFloat num = numStr.floatValue;
	int fullNum = (int)num;
	BOOL isHalf = (CGFloat)(num - fullNum)>0.1?YES:NO;
	for (UIImageView *starIV in _starBaseView.subviews) {
		starIV.image = [UIImage imageNamed:@"star_empty.png"];
		if (starIV.tag<=fullNum) {
			starIV.image = [UIImage imageNamed:@"star_full.png"];
		}
		else if (starIV.tag == fullNum + 1 && isHalf) {
			starIV.image = [UIImage imageNamed:@"star_half.png"];
		}
	}
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
